package pers.wensheng.patterns.structural_pattern.flyweight_pattern;

import pers.wensheng.patterns.structural_pattern.flyweight_pattern.entity.AbstractBox;
import pers.wensheng.patterns.structural_pattern.flyweight_pattern.entity.IBox;
import pers.wensheng.patterns.structural_pattern.flyweight_pattern.entity.LBox;
import pers.wensheng.patterns.structural_pattern.flyweight_pattern.entity.OBox;

import java.util.HashMap;

/**
 * 工厂类
 * 将该类设计为单例
 */
public class BoxFactory {
    /**
     * 单例的
     */
    private static BoxFactory boxFactory = new BoxFactory();
    /**
     * 存取所有的对象
     * 在使用享元模式时需要维护一个存储享元对象的享元池
     */
    private HashMap<String, AbstractBox> map;

    /**
     * ---------构造方法------------
     */
    private BoxFactory(){
        map = new HashMap<>();
        map.put("I",new IBox());
        map.put("L",new LBox());
        map.put("O",new OBox());
    }

    /**
     * 获取工厂对象
     * @return
     */
    public static BoxFactory getInstance(){
        return boxFactory;
    }

    /**
     * 根据名称获取图形对象
     * 返回值享元对象
     */
    public AbstractBox getShape(String name){
        return map.get(name);
    }
}
